package com.ruoyi.inventory.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.TWhWarehouseMapper;
import com.ruoyi.inventory.domain.TWhWarehouse;
import com.ruoyi.inventory.service.ITWhWarehouseService;
import com.ruoyi.dict.service.IDictUsageCheckService;
import com.ruoyi.common.exception.ServiceException;

/**
 * 仓库Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class TWhWarehouseServiceImpl implements ITWhWarehouseService 
{
    @Autowired
    private TWhWarehouseMapper tWhWarehouseMapper;
    
    @Autowired
    private IDictUsageCheckService dictUsageCheckService;

    /**
     * 查询仓库
     * 
     * @param code 仓库主键
     * @return 仓库
     */
    @Override
    public TWhWarehouse selectTWhWarehouseByCode(String code)
    {
        return tWhWarehouseMapper.selectTWhWarehouseByCode(code);
    }

    /**
     * 查询仓库列表
     * 
     * @param tWhWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<TWhWarehouse> selectTWhWarehouseList(TWhWarehouse tWhWarehouse)
    {
        return tWhWarehouseMapper.selectTWhWarehouseList(tWhWarehouse);
    }

    /**
     * 新增仓库
     * 
     * @param tWhWarehouse 仓库
     * @return 结果
     */
    @Override
    public int insertTWhWarehouse(TWhWarehouse tWhWarehouse)
    {
        return tWhWarehouseMapper.insertTWhWarehouse(tWhWarehouse);
    }

    /**
     * 修改仓库
     * 
     * @param tWhWarehouse 仓库
     * @return 结果
     */
    @Override
    public int updateTWhWarehouse(TWhWarehouse tWhWarehouse)
    {
        return tWhWarehouseMapper.updateTWhWarehouse(tWhWarehouse);
    }

    /**
     * 批量删除仓库
     * 
     * @param codes 需要删除的仓库主键
     * @return 结果
     */
    @Override
    public int deleteTWhWarehouseByCodes(String[] codes)
    {
        // 检查每个备件仓库是否被使用
        for (String code : codes) {
            if (dictUsageCheckService.isWarehouseInUse(code)) {
                String description = dictUsageCheckService.getWarehouseUsageDescription(code);
                throw new ServiceException(description);
            }
        }
        return tWhWarehouseMapper.deleteTWhWarehouseByCodes(codes);
    }

    /**
     * 删除仓库信息
     * 
     * @param code 仓库主键
     * @return 结果
     */
    @Override
    public int deleteTWhWarehouseByCode(String code)
    {
        // 检查备件仓库是否被使用
        if (dictUsageCheckService.isWarehouseInUse(code)) {
            String description = dictUsageCheckService.getWarehouseUsageDescription(code);
            throw new ServiceException(description);
        }
        return tWhWarehouseMapper.deleteTWhWarehouseByCode(code);
    }
    
    /**
     * 物理删除仓库信息（允许重复编码）
     * 
     * @param code 仓库主键
     * @return 结果
     */
    public int physicallyDeleteTWhWarehouseByCode(String code)
    {
        return tWhWarehouseMapper.physicallyDeleteTWhWarehouseByCode(code);
    }
}